<template>
  <div class="login-box">
    <div class="login-inner">
      <!-- 1.0 标题部分 -->
      <h3>超级系统登录</h3>
      <!-- 2.0 表单部分 -->
      <el-form
        :model="loginForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
        size="mini"
      >
        <el-form-item prop="username">
          <el-input
            type="text"
            v-model="loginForm.username"
            autocomplete="off"
            prefix-icon="iconfont icon-yonghu"
          ></el-input>
        </el-form-item>
        <el-form-item prop="pwd">
          <el-input
            :type="flag ? 'text' : 'password'"
            v-model="loginForm.pwd"
            autocomplete="off"
            prefix-icon="iconfont icon-mima"
            @keyup.native.13.once="submitForm('ruleForm')"
          >
            <!-- <template v-slot:suffix> -->
            <template #suffix>
              <i
                class="iconfont"
                :class="flag ? 'icon-eyes-' : 'icon-eyes'"
                @click="flag = !flag"
              ></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="login-btn"
            @click="submitForm('ruleForm')"
            :loading="isClick"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { validateUserName, validatePWD } from '../utils/validate.js'
// 引入api中的接口
import { login } from '../api/users.js'
import local from '../utils/local.js'
import { createRoute } from '../router/index.js'

export default {
  data() {
    return {
      isClick: false,
      flag: false,
      loginForm: {
        username: '小貂蝉',
        pwd: '666'
      },
      rules: {
        // validator 校验器   trigger 触发条件
        username: [{ validator: validateUserName, trigger: 'blur' }],
        pwd: [{ validator: validatePWD, trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.isClick = true
      // this.$refs.ruleForm  !==  this.$refs.formName
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 这里代表验证已经通过了，可以发请求了
          const data = await login({
            account: this.loginForm.username,
            password: this.loginForm.pwd
          })
          // console.log(data)
          if (data.code === 0) {
            // 跳转到首页
            setTimeout(() => {
              this.isClick = false
              // 把请求成功的 token 保存到本地存储中
              local.set('token', data.token)
              local.set('role', data.role)
              createRoute()
              this.$router.push('/')
            }, 2000)
          } else {
            this.isClick = false
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
h1 {
  color: red;
}

.login-box {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #2d3a4b;

  .login-inner {
    width: 350px;
    height: 270px;

    h3 {
      padding: 20px 0;
      text-align: center;
      font-weight: 700;
      letter-spacing: 3px;
      color: #fff;
    }

    .login-btn {
      width: 100%;
      letter-spacing: 5px;
    }

    /* 穿刺的写法 就是让编译之后的元素也能执行这个样式 */
    /deep/ .el-input {
      input {
        color: #fff;
        background-color: #2d3a4b;
      }
    }
  }
}
</style>
